package dingService

import (
	dingtalkworkflow_1_0 "github.com/alibabacloud-go/dingtalk/workflow_1_0"
	"gorm.io/gorm"
	"wonlicsys/database/dbModle"
	"wonlicsys/pkg/dingding"
	"wonlicsys/utils/tool"
)

//CreateOaTempalte 创建审批模版

func (oa *ResCreatOrUpdateTemplateStruct) CreateOaTempalte(db *gorm.DB, reqTextFileComponents ReqCreatOrUpdateTemplateStruct) error {
	var (
		err                error
		TexFieldComponents dingding.TexFieldComponents
		PhotoComponents    dingding.PhotoFieldComponents
		AllCompant         []*dingtalkworkflow_1_0.FormComponent
	)

	for _, component := range reqTextFileComponents.OaComponent {
		switch component.ComponentType {
		case "TextField":
			if err = tool.StructToStruct(component, &TexFieldComponents); err != nil {
				return err
			}
			AllCompant = append(AllCompant, dingding.GetTextFieldComponents(&TexFieldComponents))
		case "DDPhotoField":
			if err = tool.StructToStruct(component, &PhotoComponents); err != nil {
				return err
			}
			AllCompant = append(AllCompant, dingding.GetPhotoField(&PhotoComponents))
		}
	}

	dinService := dingding.GetInstance()
	oa.ProcessCode, err = dinService.CreateWorkFlowForms(reqTextFileComponents.ProcessCode, reqTextFileComponents.OaTempName, reqTextFileComponents.OsTempDescription, AllCompant)
	if err != nil {
		return err
	}
	//创建模版
	//创建模版
	var oaTemp dbModle.OaTemplate
	oaTemp.OaTempName = reqTextFileComponents.OaTempName
	oaTemp.OaTempDescription = reqTextFileComponents.OsTempDescription
	oaTemp.OaTempProcessCode = oa.ProcessCode
	db.Where("oa_temp_process_code = ?", oaTemp.OaTempProcessCode).FirstOrCreate(&oaTemp)

	//创建子组件
	var oaAllComponent []dbModle.OaComponent
	for _, component := range AllCompant {
		var oaComponent dbModle.OaComponent
		oaComponent.OaTempId = oaTemp.OaTempId
		oaComponent.OaFormComponentId = *component.Props.ComponentId
		oaComponent.OaFormComponentName = *component.Props.Label
		oaComponent.OaFormComponentType = *component.ComponentType
		oaAllComponent = append(oaAllComponent, oaComponent)

	}
	if err = db.Where("oa_temp_id =?", oaTemp.OaTempId).Delete(&dbModle.OaComponent{}).Error; err != nil {
		return err
	}

	if err = db.Create(&oaAllComponent).Error; err != nil {
		return err
	}

	return nil
}

//StartProcessInstances 开启审批
func (oa *ResStartProcessInstances) StartProcessInstances(db *gorm.DB, reqStartInstance ReqProcessInstances) error {
	var (
		allFormComponentValues0Details []*dingtalkworkflow_1_0.StartProcessInstanceRequestFormComponentValues
		err                            error
		oaInstanceId                   string
	)
	//控件数据转换
	if err = tool.StructToStruct(reqStartInstance.StartFormComponentValues, &allFormComponentValues0Details); err != nil {
		return err
	}
	//发送审批
	dinService := dingding.GetInstance()
	if oaInstanceId, err = dinService.WorkFlowProcessInstances(reqStartInstance.OriginatorUserId, reqStartInstance.ProcessCode, reqStartInstance.ActionType, reqStartInstance.UserIds, allFormComponentValues0Details); err != nil {
		return err
	}
	//存储库
	oaProcessInstances := dbModle.OaProcessInstances{
		OaTempId:           reqStartInstance.OaTempId, // 模版id
		OaInstanceId:       oaInstanceId,              // 申请实例id
		OaProcessCode:      oaInstanceId,              // processCode
		OaOriginatorUserId: reqStartInstance.OriginatorUserId,
	}
	if err = db.Create(&oaProcessInstances).Error; err != nil {
		return err
	}

	return nil
}
